package com.angel.domain.angeluser.entities;

import lombok.Getter;

import java.util.LinkedList;
import java.util.List;

/*
 * 更新关注微信公众号： 为了offer
 * @author parkin
 * @description
 * @date 2021/11/13
 */
public class AngelLevel {
    @Getter
    private String angelLevelId;
    // 等级名称
    @Getter
    private String angelLevelName;
    // 经验值
    @Getter
    private Integer experienceValue;

    // 等级链表
    private static List<AngelLevel> angelLevelChain;

    private static final AngelLevel TAIL =
            new AngelLevel("tail","tail",null);

    private AngelLevel(String angelLevelName, String angelLevelId,Integer experienceValue) {
        this.angelLevelId = angelLevelId;
        this.angelLevelName = angelLevelName;
        this.experienceValue = experienceValue;
    }

    private AngelLevel() {}

    public static AngelLevel toAngelLevel(String angelLevelId,String angelLevelName,Integer experienceValue) {
        AngelLevel angelLevel = new AngelLevel();
        angelLevel.angelLevelId = angelLevelId;
        angelLevel.angelLevelName = angelLevelName;
        angelLevel.experienceValue = experienceValue;
        return angelLevel;
    }

    /**
     * 实习天使-刚出生的天使
     */
    public static final AngelLevel INTERNSHIP_ANGEL = new AngelLevel(
            "实习天使",
            "Internship",
            10
    );

    /**
     * 一般天使-亡魂领导者, 等级最低
     */
    public static final AngelLevel GENERALLY_ANGEL = new AngelLevel(
            "普通天使",
            "GENERALLY",
            100
    );

    static {
        angelLevelChain = new LinkedList<>();
        angelLevelChain.add(INTERNSHIP_ANGEL);
        angelLevelChain.add(GENERALLY_ANGEL);
    }

    public AngelLevel getNextAngelLevelOfNow() {
        return getNextAngelLevel(this);
    }

    public AngelLevel getNextAngelLevel(AngelLevel nowAngelLevel) {

        int nowLevelIndexOfChain = angelLevelChain.indexOf(nowAngelLevel);
        if (nowLevelIndexOfChain == -1) {
            throw new RuntimeException("Invalid parameter, this level does not exist");
        }

        if ((nowLevelIndexOfChain+=1) == angelLevelChain.size()) {
            return TAIL;
        }

        AngelLevel nextAngelLevel = angelLevelChain.get(
                nowLevelIndexOfChain
        );

        return nextAngelLevel;
    }

    // 提升经验值
    public void boostExperienceValue(Integer experienceValue) {
        this.experienceValue += experienceValue;
    }
}
